package com.example.nophase.common;

import java.io.IOException;

import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;

/**
 * 对头信息进行统一处理
 *
 */
public class HeaderInterceptor implements Interceptor {
    private String token = null;

    private ITokenTimeout iTokenTimeout;

    public HeaderInterceptor(String token, ITokenTimeout iTokenTimeout) {
        this.token = token;
        this.iTokenTimeout = iTokenTimeout;

    }

    @Override
    public Response intercept(Chain chain) throws IOException {
        Request.Builder builder = chain.request().newBuilder();
        builder.addHeader("Accept","application/json")
                .addHeader("Client-Type","ANDROID");
        if(token!=null){
            builder.addHeader("Authorization", "Bearer "+token);
            System.out.println("token======="+token);
        }
        Response response = chain.proceed(builder.build());
        if(response.code()==401){
            iTokenTimeout.onTimeout();
        }
        return response;
    }
}
